<?php

namespace App\Admin\Forms\User;

use App\Models\User\WxUser;
use App\Wen\Utils\UserUtils;
use Carbon\Carbon;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\Cache;

class UserBanAndMuteForm extends Form implements LazyRenderable
{
    use LazyWidget;

    /**
     * 处理表单请求.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        $user_id = $input['id'];
        $user_state = $input['user_state'];
        $time_until = $input['time_until'];
        $black_reason = strip_tags(trim($input['black_reason']));

        if($user_state == 2 || $user_state == 3){
            if($time_until){
                $the_user = WxUser::find($user_id);
                $timestamp = Carbon::parse($time_until)->timestamp;
                if($user_state == 2){
                    if(UserUtils::lock_user($the_user, 2, $timestamp, $black_reason)){
                        return $this->response()
                            ->success('已将该用户封号至'.$time_until)
                            ->refresh();
                    }else{
                        return $this->response()
                            ->error('失败');
                    }
                }else if($user_state == 3){
                    if(UserUtils::lock_user($the_user, 3, $timestamp)){
                        return $this->response()
                            ->success('已将该用户禁言至'.$time_until)
                            ->refresh();
                    }else{
                        return $this->response()
                            ->error('失败');
                    }
                }
            }else{
                return $this->response()
                    ->error('请配置限制到什么时间');
            }
        }else{
            if($user_state == 1){

            }else{
                $old_user = WxUser::find($user_id);
                if($old_user->user_state == 2){
                    if(UserUtils::unlock_user($old_user, 2)){
                        return $this->response()
                            ->success('已解除封号')
                            ->refresh();
                    }else{
                        return $this->response()
                            ->error('失败');
                    }
                }else if($old_user->user_state == 3){
                    if(UserUtils::unlock_user($old_user, 3)){
                        return $this->response()
                            ->success('已解除禁言')
                            ->refresh();
                    }else{
                        return $this->response()
                            ->error('失败');
                    }
                }
            }

        }
    }

    /**
     * 构建表单.
     */
    public function form()
    {
        $user_id = $this->payload["id"];
        $this->hidden('id')->value($user_id);
        $user = WxUser::find($user_id);
        $time_until = '';
        if($user->user_state == 2){
            $ban_until = get_user_meta($user->id, 'ban_until', 's');
            if(time() > $ban_until){
                // 解除封号
                if(UserUtils::unlock_user($user, 2)){
                    $user = WxUser::find($user_id);
                    $time_until = '';
                }else{
                    $time_until = Carbon::createFromTimestamp($ban_until)->toDateTimeString();
                }
            }else{
                $time_until = Carbon::createFromTimestamp($ban_until)->toDateTimeString();
            }

        }else if($user->user_state == 3){
            $mute_until = get_user_meta($user->id, 'mute_until', 's');
            if(time() > $mute_until){
                if(UserUtils::unlock_user($user, 3)){
                    $user = WxUser::find($user_id);
                    $time_until = '';
                }else{
                    $time_until = Carbon::createFromTimestamp($mute_until)->toDateTimeString();
                }
            }else{
                $time_until = Carbon::createFromTimestamp($mute_until)->toDateTimeString();
            }

        }


        $this->radio('user_state')->options([0=>'正常',1=>'用户注销',2=>'管理员封号', 3=>'禁言'])->default($user->user_state)->when([2, 3], function (Form $form) use (&$user, &$time_until){
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;display: inline-block">以下针对于封号/禁言</div>';
            }, '');
            $form->text('black_reason')->default($user->black_reason);

            $form->datetime('time_until', '限制至')->default( $time_until );
        });



    }
}
